package com.sservice.web.common.util.net;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class HttpRequest {
    private static final Object HTTP = "";
    private static Logger logger = LoggerFactory.getLogger(HttpRequest.class);
    private static String DOMAIN = "http://" + "123"; //

    public static String doPost(String url, String param) {
        String uriAPI = DOMAIN + url;//Post方式没有参数在这里
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uriAPI);
            StringEntity se = new StringEntity(param, "utf-8");
            se.setContentEncoding("UTF-8");
            httpPost.setEntity(se);
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            return getResult(httpResponse);
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doPost(String url, Param param) {
        String uriAPI = DOMAIN + url;//Post方式没有参数在这里
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uriAPI);
            StringEntity se = new StringEntity(JSONObject.toJSONString(param), "utf-8");
            se.setContentEncoding("UTF-8");
            httpPost.setEntity(se);
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            return getResult(httpResponse);
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doGet(String url, Param param) {
        String uriAPI = DOMAIN + url;//Post方式没有参数在这里
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGetWithBody httpGet = new HttpGetWithBody(uriAPI);
            StringEntity se = new StringEntity(JSONObject.toJSONString(param), "utf-8");
            se.setContentEncoding("UTF-8");
            httpGet.setEntity(se);
            httpGet.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpGet);
            return getResult(httpResponse);
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doGet(String url, String param) {
        String uriAPI = url;

        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            logger.error(uriAPI);
            HttpGetWithBody httpGet = new HttpGetWithBody(uriAPI);
            if (!"".equals(param)) {
                StringEntity se = new StringEntity(param, "utf-8");
                se.setContentEncoding("UTF-8");
                httpGet.setEntity(se);
            }
            httpGet.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpGet);
            return getResult(httpResponse);
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doDelete(String url, Param param) {
        String uriAPI = DOMAIN + url;
        System.out.println(uriAPI);
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(uriAPI);
            StringEntity se = new StringEntity(JSONObject.toJSONString(param), "utf-8");
            se.setContentEncoding("UTF-8");
            httpDelete.setEntity(se);
            httpDelete.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpDelete);
            return getResult(httpResponse);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doPatch(String url, Param param) {
        String uriAPI = DOMAIN + url;
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPatch httpPatch = new HttpPatch(uriAPI);
            StringEntity se = new StringEntity(JSONObject.toJSONString(param), "utf-8");
            se.setContentEncoding("UTF-8");
            httpPatch.setEntity(se);
            httpPatch.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPatch);
            return getResult(httpResponse);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    public static String doPatch(String url, String param) {
        String uriAPI = DOMAIN + url;
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPatch httpPatch = new HttpPatch(uriAPI);
            StringEntity se = new StringEntity(param, "utf-8");
            se.setContentEncoding("UTF-8");
            httpPatch.setEntity(se);
            httpPatch.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPatch);
            return getResult(httpResponse);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage().toString();
        }
        return result;
    }

    private static String getResult(HttpResponse httpResponse) throws IOException {
        String result = "";
//        if (httpResponse.getStatusLine().getStatusCode() == 200 || httpResponse.getStatusLine().getStatusCode() == 500|| httpResponse.getStatusLine().getStatusCode() == 400) {
        HttpEntity httpEntity = httpResponse.getEntity();
        result = EntityUtils.toString(httpEntity);//取出应答字符串
        return result;
//        }
//        return result;
    }

    /***
     * chong
     * @return
     * @throws IOException
     */
    public static JSONObject reformatRes(String res) throws IOException {
        String result = "";
        if (res.indexOf("{") == 0) {
            return JSONObject.parseObject(res);
        }
        if (res.indexOf("[") == 0) {
            return JSONArray.parseObject(res);
        }
        return JSONObject.parseObject("{\"error\":\"" + res + "\"}");
    }

}
